<?php

/**
 * statement actions.
 *
 * @package    LYPO
 * @subpackage statement
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class statementActions extends sfActions
{

	public function preExecute(){
		$this->setTemplate('result');
	}

	public function executeList(sfWebrequest $request)
	{
		/* @var $statement LypoStatement */
		$result = new LypoResult();
		$statements = array();
		$ret = LypoStatementPeer::getAllStatements();
		foreach($ret as $statement){
			$statements[]= $statement->toArray(BasePeer::TYPE_FIELDNAME);
		}
		$result->setData('statements',$statements);
		$this->result = $result;
		//return $this->renderText($result->toJSON());
	}	
	
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeRegistration(sfWebrequest $request)
	{
		/* @var $statement LypoStatement */
		$result = new LypoResult();
		$statements = array();
		$ret = LypoStatementPeer::getRegistrationStatements();
		foreach($ret as $statement){
			$statements[]= $statement->toArray(BasePeer::TYPE_FIELDNAME);
		}
		$result->setData('statements',$statements);
		$this->result = $result;
		//return $this->renderText($result->toJSON());
	}

	public function executeSaveanswer($request)
	{
		$userid = $request->getParameter('userid');
		$statement_id = $request->getParameter('statement_id');
		$answer = $request->getParameter('answer');
		/* @var $statement LypoStatement */
		$result = new LypoResult();
		if($userid && $statement_id && $answer){
			try {
				/* @var $user User */
				$user = UserPeer::getByContainerString($userid);
				if($user){
					$ret = StatementAnswerPeer::saveAnswer($statement_id, $answer, $user);
					StatementAnswerPeer::createUsersAnswersFromLypoSignature($user->getId(),$user->getLypoSignature());
					$result->setData('lypo_signature',$user->getLypoSignature());
					$result->setMessage('OK');
				}else{
					$result->setErrorMessage("User not found");
				}
			}catch (Exception $e){
				$result->setMessage($e->getMessage());
			}
				
		}else{
			$result->setErrorMessage("Not all parameters present in request (userid, statement_id,answer)");
		}
		$this->result = $result;
		//return $this->renderText($result->toJSON());
	}

	


}
